import { saveAs } from 'file-saver';
const XLSX = require('xlsx');
const XLSXStyle = require('xlsx-style');
window.XLSX = XLSX;
export default function exportExcel(tableid, fileName = '导出文件.xlsx', sheetName = '标签页1') {
  const table = document.getElementById(tableid).children[0].children[0].children[0];
  // 获取表头的列名
  const header = Array.from(table.querySelectorAll('th')).map((th) => th.innerText);
  // 设置表头居中的样式
  const headerStyle = {
    // alignment: { horizontal: 'center' },
    font: { bold: true },
  };

  // 设置工作表的列宽
  const columnWidths = header.map(() => ({ width: 15 }));
  var wsheader = XLSX.utils.table_to_sheet(table, {
    header: header,
    styles: { header: headerStyle },
    columnWidths: columnWidths,
  });
  // 单元格居中
  var styleCenter = Object.assign(
    {},
    {
      alignment: {
        vertical: 'center',
        horizontal: 'center',
      },
    },
  );
  //header居中
  for (let key in wsheader) {
    if (key.indexOf('!') !== 0) {
      wsheader[key]['s'] = styleCenter;
    }
  }
  let headerLen = Number(wsheader['!ref'].split(':')[1].replace(/[^0-9]/gi, '')) - 1;

  let newwsbody = {};
  var wsbody = {};
  let bodylen = 0;
  if (document.getElementById(tableid).children[0].children[1]) {
    wsbody = document.getElementById(tableid).children[0].children[1]
      ? XLSX.utils.table_to_sheet(
          document.getElementById(tableid).children[0].children[1].children[0],
        )
      : {};
    bodylen = Number(wsbody['!ref'].split(':')[1].replace(/[^0-9]/gi, ''));

    let bodyKeys = Object.keys(wsbody);

    bodyKeys.map((e, i) => {
      if (e != '!ref') {
        let obj = wsbody[e];

        let number = Number(e.replace(/[^0-9]/gi, ''));
        let zm = e.replace(number, '');
        let newzm = zm.charCodeAt(0) + headerLen;
        let key = zm + (number + headerLen);
        newwsbody[key] = obj;
      }
    });
  }

  var endC = wsheader['!ref']
    .split(':')[1]
    .replace(wsheader['!ref'].split(':')[1].replace(/[^0-9]/gi, ''), '');
  let mergea = wsheader['!merges'].concat([]);
  let mergeb = (wsbody['!merges'] || []).concat([]);
  mergeb.map((e, i) => {
    mergeb[i]['e']['r'] = mergeb[i]['e']['r'] + headerLen;
    mergeb[i]['s']['r'] = mergeb[i]['s']['r'] + headerLen;
  });
  let colswidth = [];
  for (var i = 0; i < 40; i++) {
    colswidth.push({ wch: 20 });
  }
  var renderws = {
    ...wsheader,
    ...newwsbody,
    ...{
      '!ref': 'A1:' + endC + (headerLen + bodylen),
      '!merges': mergea.concat(mergeb),
      '!cols': colswidth,
    },
  };

  // 创建边框样式对象
  const borderStyle = {
    top: { style: 'thin' },
    bottom: { style: 'thin' },
    left: { style: 'thin' },
    right: { style: 'thin' },
  };
  var wb = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(wb, renderws, sheetName);
  // 导出Excel文件
  // XLSX.writeFile(wb, fileName);

  const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
  const excelData = new Blob([excelBuffer], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8',
  });

  saveAs(excelData, fileName); // 导出文件的名称
}
